﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;

namespace OpticalPowerMeter
{
    /// <summary>
    /// 用于保存即获取检查组信息
    /// </summary>
    public class CheckGroupManagement
    {
        private static CheckGroupManagement groupManagement = null;

        private CheckGroupManagement() { }

        /// <summary>
        /// 获取实例
        /// </summary>
        /// <returns></returns>
        public static CheckGroupManagement GetInstance()
        {
            if (groupManagement == null)
            {
                groupManagement = new CheckGroupManagement();
            }
            return groupManagement;
        }


        //配置文件保存的完整名称
        private string FileName = System.Environment.CurrentDirectory + "\\Config\\" + "CheckGroup.xml";
        private string rootNodeName = "CheckGroups"; //根节点名称
        private string groupNodeName = "CheckGroup"; //检查组节点名称
        private string checkListNodeName = "CheckList"; //检查点清单

        private void InitFile()
        {
            string path = System.Environment.CurrentDirectory + "\\Config";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            #region 如果文件不存在，添加文件
            if (!File.Exists(FileName))
            {
                XDocument xDoc = new XDocument(
                    new XDeclaration("1.0", "UTF-8", "yes"),
                    new XElement(rootNodeName));
                xDoc.Save(FileName);
            }
            #endregion
        }

        /// <summary>
        /// 保存波长对应的检查点
        /// </summary>
        ///<param name="groupname">检查组名称</param>
        ///<param name="checkListDic">波长对应的检查清单</param>
        public void SaveCheckList(string groupname,Dictionary<string,List<CheckPoint>> checkListDic)
        {
            InitFile();
            XElement root = XElement.Load(FileName);
            XElement groupElement = (from element in root.Elements(groupNodeName)
                                     where element.Attribute("Name") != null &&
                                     element.Attribute("Name").Value.Equals(groupname)
                                     select element)?.SingleOrDefault();
            #region 如果存在该节点，则清除下面的所有子节点，否则新建一个节点
            if (groupElement != null)
            {
                groupElement.RemoveNodes();
            }
            else
            {
                groupElement = new XElement(groupNodeName,new XAttribute("Name",groupname));
                root.Add(groupElement);
            }
            #endregion
            //将检查点清单保存在本地
            foreach (string wavelength in checkListDic.Keys) {
                XElement wavelengthElement = new XElement(checkListNodeName, new XAttribute("Wavelength",wavelength));
                List<CheckPoint> checklist = checkListDic[wavelength];
                foreach(CheckPoint check in checklist)
                {
                    wavelengthElement.Add(new XElement("Check",new XAttribute("Power",check.CheckPoint_P),new XAttribute("LimitH",check.Limit_H),new XAttribute("LimitL",check.Limit_L)));
                }
                groupElement.Add(wavelengthElement);
            }
            root.Save(FileName);
        }

        /// <summary>
        /// 删除检查组
        /// </summary>
        /// <param name="groupName">检查组名称</param>
        public void DeleteGroup(string groupname) {
            InitFile();
            XElement root = XElement.Load(FileName);
            XElement groupElement = (from element in root.Elements(groupNodeName)
                                     where element.Attribute("Name") != null &&
                                     element.Attribute("Name").Value.Equals(groupname)
                                     select element)?.SingleOrDefault();
            if (groupElement != null)
            {
                groupElement.Remove();
                root.Save(FileName);
            }
        }

        /// <summary>
        /// 获取指定组下指定波长的检查清单
        /// </summary>
        /// <param name="groupname">组名</param>
        /// <param name="wavelength">波长</param>
        /// <returns>检查清单</returns>
        public List<CheckPoint> GetCheckList(string groupname,string wavelength) {
            List<CheckPoint> checkList = new List<CheckPoint>();
            InitFile();
            XElement root = XElement.Load(FileName);
            XElement groupElement = (from element in root.Elements(groupNodeName)
                                     where element.Attribute("Name") != null &&
                                     element.Attribute("Name").Value.Equals(groupname)
                                     select element)?.SingleOrDefault();
            if (groupElement == null)
            {
                return checkList;
            }
            #region 查询是否存在相应的波长检查清单
            XElement wavelengthElement = (from element in groupElement.Elements(checkListNodeName)
                                          where element.Attribute("Wavelength") != null &&
                                          element.Attribute("Wavelength").Value.Equals(wavelength)
                                          select element)?.SingleOrDefault();
            if (wavelengthElement != null)
            {
                foreach (XElement checkElement in wavelengthElement.Elements("Check")) {
                    string power = checkElement.Attribute("Power").Value;
                    string limitH = checkElement.Attribute("LimitH").Value;
                    string limitL = checkElement.Attribute("LimitL").Value;
                    CheckPoint check = new CheckPoint();
                    check.CheckPoint_P = Convert.ToDouble(power);
                    check.Limit_H = Convert.ToDouble(limitH);
                    check.Limit_L = Convert.ToDouble(limitL);
                    checkList.Add(check);
                }
            }
            return checkList;
            #endregion
        }

        /// <summary>
        /// 获取检查组下的波长清单
        /// </summary>
        /// <param name="groupname">检查组名</param>
        /// <returns>波长列表</returns>
        public List<string> GetWavelengthList(string groupname) {
            List<string> wavelengthList = new List<string>();
            InitFile();
            XElement root = XElement.Load(FileName);
            XElement groupElement = (from element in root.Elements(groupNodeName)
                                     where element.Attribute("Name") != null &&
                                     element.Attribute("Name").Value.Equals(groupname)
                                     select element)?.SingleOrDefault();
            if (groupElement == null)
            {
                return wavelengthList;
            }
            foreach(XElement checkListElement in groupElement.Elements(checkListNodeName))
            {
                string wavelength = checkListElement.Attribute("Wavelength").Value;
                wavelengthList.Add(wavelength);
            }
            return wavelengthList;
        }


        /// <summary>
        /// 获取检查组清单
        /// </summary>
        /// <returns>检查组名清单</returns>
        public List<string> GetGroupnameList()
        {
            List<string> groupnameList = new List<string>();
            InitFile();
            XElement root = XElement.Load(FileName);
            var result = (from elements in root.Elements(groupNodeName)
                          select elements);
            if (result.Count() > 0)
            {
                foreach (XElement groupnameElement in result) {
                    string groupname = groupnameElement.Attribute("Name")?.Value;
                    if (groupname != null)
                    {
                        groupnameList.Add(groupname);
                    }
                }
            }
            return groupnameList;
        }
    }
}
